﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;

namespace OnlineCooperation.DB
{
    public class datasetMgn
    {
        //从数据库中读取数据，返回一个DataTable对象
        public DataTable ReadTable(String strSql, String strConn)
        {
            strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            DataTable dt = new DataTable();
            MySqlConnection Conn = new MySqlConnection(strConn);
            Conn.Open();
            MySqlDataAdapter Cmd = new MySqlDataAdapter(strSql, Conn);
            Cmd.Fill(dt);
            Conn.Close();
            return dt;
        }

        //从数据库中读取数据，返回一个DataSet对象
        public DataSet ReadDataSet(String strSql, String strConn)
        {
            strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            DataSet ds = new DataSet();
            MySqlConnection Conn = new MySqlConnection(strConn);
            Conn.Open();
            MySqlDataAdapter Cmd = new MySqlDataAdapter(strSql, Conn);
            Cmd.Fill(ds);
            Conn.Close();
            return ds;
        }

        //比ReadDataSet多了一个tableName参数
        public DataSet GetDataSet(String strSql, String tableName, String strConn)
        {
            strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            DataSet ds = new DataSet();
            MySqlConnection Conn = new MySqlConnection(strConn);
            Conn.Open();
            MySqlDataAdapter Cmd = new MySqlDataAdapter(strSql, Conn);
            Cmd.Fill(ds, tableName);
            Conn.Close();
            return ds;
        }

        //SQL查询，返回Reader对象
        public MySqlDataReader readrow(String sql, String strConn)
        {
            strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            MySqlConnection Conn = new MySqlConnection(strConn);
            Conn.Open();
            MySqlCommand Comm = new MySqlCommand(sql, Conn);
            MySqlDataReader Reader = Comm.ExecuteReader();
            if (Reader.Read())
            {
                Comm.Dispose();
                return Reader;
            }
            else
            {
                Comm.Dispose();
                return null;
            }
        }

        //返回查询结果第一行某一个字段的值
        public String Readstr(String strSql, int flag, String strConn)
        {
            strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            DataSet ds = new DataSet();
            String str;
            MySqlConnection Conn = new MySqlConnection(strConn);
            Conn.Open();
            MySqlDataAdapter Cmd = new MySqlDataAdapter(strSql, Conn);
            Cmd.Fill(ds);
            str = ds.Tables[0].Rows[0].ItemArray[flag].ToString();
            Conn.Close();
            return str;
        }

        //执行SQL更新语句
        public void execsql(String strSql, String strConn)
        {
            strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            MySqlConnection Conn = new MySqlConnection(strConn);
            MySqlCommand Comm = new MySqlCommand(strSql, Conn);
            Conn.Open();
            Comm.ExecuteNonQuery();
            Conn.Close();
        }
    }
}